using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class CashHistoryDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CashHistoryDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CashHistory table.
		/// </summary>
		public virtual void Insert(CashHistoryInfo cashHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", cashHistoryInfo.IdCash),
				new SqlParameter("@PostDate", cashHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", cashHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", cashHistoryInfo.AccountNo),
				new SqlParameter("@Cash", cashHistoryInfo.Cash),
				new SqlParameter("@SecType", cashHistoryInfo.SecType)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CashHistory_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CashHistory table.
		/// </summary>
		public virtual void Update(CashHistoryInfo cashHistoryInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", cashHistoryInfo.IdCash),
				new SqlParameter("@PostDate", cashHistoryInfo.PostDate),
				new SqlParameter("@IdCommon", cashHistoryInfo.IdCommon),
				new SqlParameter("@AccountNo", cashHistoryInfo.AccountNo),
				new SqlParameter("@Cash", cashHistoryInfo.Cash),
				new SqlParameter("@SecType", cashHistoryInfo.SecType)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CashHistory_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CashHistory table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idCash)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", idCash)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CashHistory_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CashHistory table.
		/// </summary>
		public virtual CashHistoryInfo Select(Guid? idCash)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCash", idCash)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CashHistory_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCashHistoryInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CashHistory table.
		/// </summary>
		public List<CashHistoryInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CashHistory_SelectAll"))
			{
				List<CashHistoryInfo> cashHistoryInfoList = new List<CashHistoryInfo>();
				while (dataReader.Read())
				{
					cashHistoryInfoList.Add(MakeCashHistoryInfo(dataReader));
				}

				return cashHistoryInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CashHistory class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CashHistoryInfo MakeCashHistoryInfo(SqlDataReader dataReader)
		{
			CashHistoryInfo cashHistoryInfo = new CashHistoryInfo();
			cashHistoryInfo.IdCash = SqlClientUtility.GetGuid(dataReader, DbConstants.CASHHISTORY.IDCASH);
			cashHistoryInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.CASHHISTORY.POSTDATE);
			cashHistoryInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.CASHHISTORY.IDCOMMON);
			cashHistoryInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.CASHHISTORY.ACCOUNTNO);
			cashHistoryInfo.Cash = SqlClientUtility.GetDecimal(dataReader, DbConstants.CASHHISTORY.CASH);
			cashHistoryInfo.SecType = SqlClientUtility.GetString(dataReader, DbConstants.CASHHISTORY.SECTYPE);

			return cashHistoryInfo;
		}

		#endregion
	}
}
